import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  {
    path: '/login',
    component: resolve => require(['../views/Login.vue'], resolve)
  }, {
    path: '/home',
    component: resolve => require(['../views/Home.vue'], resolve),
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: resolve => require(['../views/Welcome.vue'], resolve) },
      { path: '/staff', component: resolve => require(['../views/staff/StaffInfo.vue'], resolve) },
      { path: '/vip', component: resolve => require(['../views/vip/CusVip.vue'], resolve) },
      { path: '/business', component: resolve => require(['../views/business/BusinessInfo.vue'], resolve) },
      {
        path: '/monthEnd',
        redirect: '/TurNover',
        component: resolve => require(['../views/monthEnd/MonthEnd.vue'], resolve),
        children: [{ path: '/TurNover', component: resolve => require(['../views/monthEnd/TurNover.vue'], resolve) },
          { path: '/StaffMon', component: resolve => require(['../views/monthEnd/StaffMon.vue'], resolve) }]
      }

    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的页面
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  // next()放行   next('/login')强制跳转
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router
